/*-------------------------------------------------------------------------
 *
 * pqsignal.c
 *	  Backend signal(2) support (see also src/port/pqsignal.c)
 *
 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  src/backend/libpq/pqsignal.c
 *
 * ------------------------------------------------------------------------
 */

#include "postgres.h"

#include "libpq/pqsignal.h"


/* Global variables */
sigset_t	UnBlockSig,			/* 不希望屏蔽的信号集合 */
			BlockSig,			/* 希望屏蔽的信号集合 */
			StartupBlockSig;	/* 启动阶段需要屏蔽的信号集合 */	


/*
 * 2024.7.26: 操作系统信号初始化，分成3个集合：阻塞信号集、不阻塞信号集、启动阻塞信号集
 *
 * 2024.8.5: 把信号集合分成3组，在启动阶段，把3组信号集合中的信号删除掉一些
 
 * Initialize BlockSig, UnBlockSig, and StartupBlockSig.
 *
 * BlockSig is the set of signals to block when we are trying to block
 * signals.  This includes all signals we normally expect to get, but NOT
 * signals that should never be turned off.
 *
 * StartupBlockSig is the set of signals to block during startup packet
 * collection; it's essentially BlockSig minus SIGTERM, SIGQUIT, SIGALRM.
 *
 * UnBlockSig is the set of signals to block when we don't want to block
 * signals.
 */
void
pqinitmask(void)
{
	/* 2024.8.5: 清空不阻塞信号集合 */
	sigemptyset(&UnBlockSig);

	/* Note: InitializeLatchSupport() modifies UnBlockSig. */

	/* 
	* First set all signals, then clear some. 
	*
	* 2024.7.24: 先将所有的信号都设置上，后面再逐一清零
	* 相当于先把开关都打开，后面再根据需要关上几个
	*/
	sigfillset(&BlockSig);
	sigfillset(&StartupBlockSig);

	/*
	 * Unmark those signals that should never be blocked. Some of these signal
	 * names don't exist on all platforms.  Most do, but might as well ifdef
	 * them all for consistency...
	 *
	 * 2024.7.24: 在启动阶段和阻塞阶段，不响应trap信号
	 */
#ifdef SIGTRAP
	sigdelset(&BlockSig, SIGTRAP);
	sigdelset(&StartupBlockSig, SIGTRAP);
#endif
	/*
	* 2024.7.24: 删除中止信号。也就是在启动阶段和阻塞阶段，不响应中止信号
	*/
#ifdef SIGABRT
	sigdelset(&BlockSig, SIGABRT);
	sigdelset(&StartupBlockSig, SIGABRT);
#endif
	/*
	* 2024.7.24: 删除杀死信号。也就是在启动阶段和阻塞阶段，不响应杀死信号
	*/
#ifdef SIGILL
	sigdelset(&BlockSig, SIGILL);
	sigdelset(&StartupBlockSig, SIGILL);
#endif
	/*
	* 2024.7.24: 删除致命的算术运算错误。也就是在启动阶段和阻塞阶段，不响应
	*/
#ifdef SIGFPE
	sigdelset(&BlockSig, SIGFPE);
	sigdelset(&StartupBlockSig, SIGFPE);
#endif
	/*
	* 2024.7.24: 删除段非法错误信号。也就是在启动阶段和阻塞阶段，不响应
	*/
#ifdef SIGSEGV
	sigdelset(&BlockSig, SIGSEGV);
	sigdelset(&StartupBlockSig, SIGSEGV);
#endif
	/*
	* 2024.7.24: 删除非法地址信号。也就是在启动阶段和阻塞阶段，不响应
	*/
#ifdef SIGBUS
	sigdelset(&BlockSig, SIGBUS);
	sigdelset(&StartupBlockSig, SIGBUS);
#endif
	/*
	* 2024.7.24: 删除非法系统调用。也就是在启动阶段和阻塞阶段，不响应
	*/
#ifdef SIGSYS
	sigdelset(&BlockSig, SIGSYS);
	sigdelset(&StartupBlockSig, SIGSYS);
#endif
	/*
	* 2024.7.24: 删除继续一个暂停的进程。也就是在启动阶段和阻塞阶段，不响应
	*/
#ifdef SIGCONT
	sigdelset(&BlockSig, SIGCONT);
	sigdelset(&StartupBlockSig, SIGCONT);
#endif

/*
* 2024.7.24: 删除退出信号。在启动阶段不响应
*/
/* Signals unique to startup */
#ifdef SIGQUIT
	sigdelset(&StartupBlockSig, SIGQUIT);
#endif

/*
* 2024.7.24: 删除中止信号。在启动阶段不响应
*/
#ifdef SIGTERM
	sigdelset(&StartupBlockSig, SIGTERM);
#endif

/*
* 2024.7.24: 删除超时告警信号。在启动阶段不响应
*/
#ifdef SIGALRM
	sigdelset(&StartupBlockSig, SIGALRM);
#endif
}
